package com.atguigu.dao.impl;

import com.atguigu.dao.ServerDao;
import com.atguigu.pojo.Server;

import java.util.List;

public class ServerDaoImpl  extends BaseDao implements ServerDao {

    @Override
    public int addServer(Server server) {
        System.out.println("ServerDaoImpl程序在["+Thread.currentThread().getName()+"]线程中");
        String sql="insert into t_server(`id`,`name`," +
                "`ip`,`ports`,`freePorts`,`cpu`,`memory`," +
                "`disk`,`endTime`) values(?,?,?,?,?,?,?,?,?);";
        return update(sql,server.getId(),server.getName(),server.getIp(),server.getPorts(),server.getFreePorts(),server.getCpu(),server.getMemory(),server.getDisk(),server.getEndTime());
    }

    @Override
    public int deleteServerById(String id) {
        String sql="delete from t_server where id=?";
        return update(sql,id);
    }

    @Override
    public int updateServer(Server server) {
        String sql="update t_server set `name`=?,`ip`=?,`ports`=?,`freePorts`=?,`cpu`=?,`memory`=?,`disk`=?,`endTime`=? where id=?";
        return update(sql,server.getName(),server.getIp(),server.getPorts(),server.getFreePorts(),server.getCpu(),server.getMemory(),server.getDisk(),server.getEndTime(),server.getId());
    }

    @Override
    public Server queryServerById(String id) {
        String sql ="select `id`,`name`,`ip`,`ports`,`freePorts`,`cpu`,`memory`,`disk`,`endTime` from t_server where id=?";
        return queryForOne(Server.class,sql,id);
    }

    @Override
    public List<Server> queryServers() {// 查询全部
        String sql ="select `id`,`name`,`ip`,`ports`,`freePorts`,`cpu`,`memory`,`disk`,`endTime` from t_server";
        return queryForList(Server.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {//查询总条目数,为page分片做准备
        String sql="select count(*) from t_server";
        Number serverCount=(Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return serverCount.intValue();
    }

    @Override
    public List<Server> queryForPageItems(Integer begin, Integer pageSize) {
        String sql="select `id`,`name`,`ip`,`ports`,`freePorts`,`cpu`,`memory`,`disk`,`endTime` from t_server limit ?,?";
        return queryForList(Server.class,sql,begin,pageSize);//查询数据片段
    }
}
